package java8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Stream用来处理数组、集合
 */
public class StreamDemo {
    public static void main(String[] args) {
        new StreamDemo().createStream5();
    }

    // 通过数组创建
    public void createStream1() {
        String[] arr = {"a", "b", "c", "d"};
        Stream<String> stream = Stream.of(arr);
    }

    // 通过集合创建
    public void createStream2() {
        String[] arr = {"a", "b", "c", "d"};
        List<String> list = Arrays.asList(arr);
        Stream<String> stream = list.stream();
    }

    // 通过Stream.generate创建
    public void createStream3() {
        Stream<Integer> stream = Stream.generate(() -> 1);
    }

    // 通过Stream.iteate创建
    public void createStream4() {
        Stream<Integer> stream =  Stream.iterate(1, x -> x + 1);
    }

    // 通过其他API创建
    public void createStream5() {
        String str = "abcd";
        IntStream intStream = str.chars();

        intStream.forEach(x -> System.out.println(x));
    }

}
